package net.atomtree.main;

import net.atomtree.controller.TestServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;

import java.io.File;
import java.io.FileInputStream;

/**
 * Created by czk on 2015/12/26.
 */
public class StartUp {
    public static void main(String[] args) {
        StartUp start=new StartUp();
        start.serverStart();
    }

    public void serverStart() {
        File file=new File(".");
        File[] files=file.listFiles();
        for(File f:files){
            System.out.println(f);
        }
        Server server = new Server(6667);
        server.setHandler(new DefaultHandler());
        XmlConfiguration configuration = null;
        try {
            configuration = new XmlConfiguration(new FileInputStream("./src/main/resources/jetty.xml"));
            configuration.configure(server);
            ServletContextHandler context = new ServletContextHandler(
                    ServletContextHandler.SESSIONS);
            WebAppContext webContext = new WebAppContext();
            webContext.setContextPath("/indexManager");
            webContext.setDescriptor("./src/main/webapp/WEB-INF/web.xml");
            webContext.setResourceBase("./src/main/webapp");
            webContext.setParentLoaderPriority(true);
//            webContext.addServlet(new ServletHolder(new ConfigurationServlet()),
//                    "/conf/*");
//            webContext.addServlet(new ServletHolder(new EngineServlet()),
//                    "/engine/*");
//            webContext.addServlet(new ServletHolder(new TestServlet()),
//                    "/test/*");
            System.out.println("init spark context");
            server.setHandler(webContext);
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
